window: Restructure gtk_window_destroy()
authorTimm Bäder <mail@baedert.org>
Wed, 13 May 2020 09:34:33 +0000 (11:34 +0200)
committerTimm Bäder <mail@baedert.org>
Wed, 13 May 2020 10:18:09 +0000 (12:18 +0200)
The previous code was unreffing the window twice, which caused problems
during dialog destruction. Move to g_list_store_find instead of
iterating manually.

ref() the window before and unref() after. g_list_store_remove will
actually unref() the window, since the toplevel_list owns its own
reference.

Fixes #2741
Fixes #2742

gtk/gtkwindow.c

index 266aeda851c43f36aeaaea4baa6509c7d0375b83..c8e51c5073519de534f158fa67470ac3c46b7551 100644 (file)
@@ -7483,23 +7483,19 @@ gtk_window_get_child (GtkWindow *window)
 void
 gtk_window_destroy (GtkWindow *window)
 {
-  int i;
+  guint i;
 
   g_return_if_fail (GTK_IS_WINDOW (window));
 
+  /* If gtk_window_destroy() has been called before. Can happen
+   * when destroying a dialog manually in a ::close handler for example. */
+  if (!g_list_store_find (toplevel_list, window, &i))
+    return;
+
+  g_object_ref (window);
   gtk_tooltip_unset_surface (GTK_NATIVE (window));
 
-  for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (toplevel_list)); i++)
-    {
-      gpointer item = g_list_model_get_item (G_LIST_MODEL (toplevel_list), i);
-      if (item == window)
-        {
-          g_list_store_remove (toplevel_list, i);
-          break;
-        }
-      else
-        g_object_unref (item);
-    }
+  g_list_store_remove (toplevel_list, i);
 
   gtk_window_hide (GTK_WIDGET (window));
   gtk_widget_unrealize (GTK_WIDGET (window));